package stdio.costa.imoca.card;

import stdio.costa.imoca.model.Image;
import stdio.costa.imoca.model.Postcard;
import stdio.costa.imoca.utils.ImageUtils;

/**
 * 最终产出物是320*460的A3标准纸张，内含8张卡片(154*111)；每行2张共4行；每行有2的印刷出血。
 * 
 * @author 张桂林
 * 
 */
public class A3PreviewBuilder extends AbstractPreviewBuilder implements PreviewBuilder {

	private Postcard[] postcards = new Postcard[8];

	public A3PreviewBuilder(Postcard c1, Postcard c2, Postcard c3, Postcard c4, Postcard c5, Postcard c6, Postcard c7, Postcard c8) {
		super();
		postcards[0] = c1;
		postcards[1] = c2;
		postcards[2] = c3;
		postcards[3] = c4;
		postcards[4] = c5;
		postcards[5] = c6;
		postcards[6] = c7;
		postcards[7] = c8;
	}

	@Override
	protected Image createFront() throws Exception {
		byte[][] rows = new byte[4][];
		for (int i = 0; i < 4; i++) {
			rows[i] = ImageUtils.combineHorizontally(320, 115, 3, 2, getFront(2 * i), getFront(2 * i + 1));
		}
		byte[] front = ImageUtils.combineVertically(320, 460, 3, 2, rows);
		return new Image(front, "image/png", null);
	}

	@Override
	protected Image createBack() throws Exception {
		byte[][] rows = new byte[4][];
		for (int i = 0; i < 4; i++) {
			//背面的左上角图片对应的是正面的右上角图片，这样正反面裁剪后才能形成正确完整的8张卡片。
			rows[i] = ImageUtils.combineHorizontally(320, 115, 3, 2, getBack(2 * i + 1), getBack(2 * i));
		}
		byte[] back = ImageUtils.combineVertically(320, 460, 3, 2, rows);
		return new Image(back, "image/png", null);
	}

	protected byte[] getBack(int i) {
		return postcards[i].getPreview().getBack().getData();
	}

	protected byte[] getFront(int i) {
		return postcards[i].getPreview().getFront().getData();
	}
}
